#include "BlockQueue.hpp"
#include "Task.hpp"
#include <ctime>


string Tohx(int x)
{
    char str[64];
    snprintf(str,64,"%x",x);

    return str;
}


void* Comsumer(void*args)
{
    BlockQueue<Task>*q=static_cast<BlockQueue<Task>*>(args);

    while(1)
    {
        Task n=q->pop();
        n();//调用了operator()()
        
        cout<<"处理任务: "<<n.GetTask()<<"运算的结果是 "<<n.GetResult()<<Tohx(pthread_self())<<endl;
        // sleep(1); //制造里面有
    }
}

void* Productor(void*args)
{
    BlockQueue<Task>*q=static_cast<BlockQueue<Task>*>(args);
    
    int size=opers.size();
    while(1)
    {
        int x=rand()%15+1;  //[1,10]
        int y=rand()%20;   //有0 的出现
        char ch =opers[rand()%size];
        Task t(x,y,ch);

        q->push(t);
        cout<<"制造了一个任务"<<t.GetTask()<<"thread-id"<<pthread_self()<<endl;
        sleep(2);
    }
}


int main()
{
    srand(time(NULL));
    BlockQueue<Task> *q=new BlockQueue<Task>(5);
    pthread_t c[3],p[5];
    for(int i=0;i<3;i++)
    {
        pthread_create(&c[i],nullptr,Comsumer,q);
    }
    for(int i=0;i<5;i++)
    {
        pthread_create(&p[i],nullptr,Productor,q);
    }

    for (int i = 0; i < 3; i++)
    {
        pthread_join(c[i], nullptr);
    }
    for (int i = 0; i < 5; i++)
    {
        pthread_join(p[i], nullptr);
    }

    delete q;
    return 0;
}